{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Left and Right Sum Differences"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #prefix-sum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #前缀和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: leftRightDifference"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #左右元素和的差值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> ，请你找出一个下标从 <strong>0</strong> 开始的整数数组 <code>answer</code> ，其中：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>answer.length == nums.length</code></li>\n",
    "\t<li><code>answer[i] = |leftSum[i] - rightSum[i]|</code></li>\n",
    "</ul>\n",
    "\n",
    "<p>其中：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>leftSum[i]</code> 是数组 <code>nums</code> 中下标 <code>i</code> 左侧元素之和。如果不存在对应的元素，<code>leftSum[i] = 0</code> 。</li>\n",
    "\t<li><code>rightSum[i]</code> 是数组 <code>nums</code> 中下标 <code>i</code> 右侧元素之和。如果不存在对应的元素，<code>rightSum[i] = 0</code> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p>返回数组 <code>answer</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [10,4,8,3]\n",
    "<strong>输出：</strong>[15,1,11,22]\n",
    "<strong>解释：</strong>数组 leftSum 为 [0,10,14,22] 且数组 rightSum 为 [15,11,3,0] 。\n",
    "数组 answer 为 [|0 - 15|,|10 - 11|,|14 - 3|,|22 - 0|] = [15,1,11,22] 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [1]\n",
    "<strong>输出：</strong>[0]\n",
    "<strong>解释：</strong>数组 leftSum 为 [0] 且数组 rightSum 为 [0] 。\n",
    "数组 answer 为 [|0 - 0|] = [0] 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 1000</code></li>\n",
    "\t<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [left-and-right-sum-differences](https://leetcode.cn/problems/left-and-right-sum-differences/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [left-and-right-sum-differences](https://leetcode.cn/problems/left-and-right-sum-differences/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[10,4,8,3]', '[1]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def leftRightDifference(self, nums: List[int]) -> List[int]:\n",
    "        left_sum,right_sum=0,sum(nums)\n",
    "        for i,x in enumerate(nums):\n",
    "            right_sum-=x\n",
    "            nums[i]=abs(left_sum-right_sum)\n",
    "            left_sum+=x\n",
    "        return nums\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def leftRightDifference(self, nums: List[int]) -> List[int]:\n",
    "        answer = nums\n",
    "        total = sum(nums)\n",
    "        lastleft = 0\n",
    "        lastright = total - nums[0]\n",
    "        lastelem = nums[0]\n",
    "        index = 0\n",
    "        for elem in nums:\n",
    "            if index == 0 or index == len(nums):\n",
    "                answer[index] = total - elem\n",
    "                index = index + 1\n",
    "                continue\n",
    "            \n",
    "            \n",
    "            left = lastleft + lastelem\n",
    "            right = lastright - elem\n",
    "            \n",
    "            answer[index] = abs(left - right)\n",
    "            index = index + 1\n",
    "            lastleft = left\n",
    "            lastright = right\n",
    "            lastelem = elem\n",
    "\n",
    "        return answer\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def leftRightDifference(self, nums: List[int]) -> List[int]:\n",
    "        l = len(nums)\n",
    "        answer = [0] * l\n",
    "        total = sum(nums)\n",
    "        lastleft = 0\n",
    "        lastright = total - nums[0]\n",
    "        lastelem = nums[0]\n",
    "        index = 0\n",
    "        for elem in nums:\n",
    "            if index == 0 or index == l:\n",
    "                answer[index] = total - elem\n",
    "                index = index + 1\n",
    "                continue\n",
    "            \n",
    "            \n",
    "            left = lastleft + lastelem\n",
    "            right = lastright - elem\n",
    "            \n",
    "            answer[index] = abs(left - right)\n",
    "            index = index + 1\n",
    "            lastleft = left\n",
    "            lastright = right\n",
    "            lastelem = elem\n",
    "\n",
    "        return answer\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
